$(document).ready(
    function(){

        //车辆提示框
        $("[id^='line_']").live("mouseover", null, function () {
            var $win = $(".detailWin"), $row = $(this);

            $win.show();
            $row.addClass("ctn-h");
            $(".detailWin").css("top", $(this).offset().top-160);
          //  vehicle_no","driver_name",  "driver_phone",
            $win.find("#vehicle_no_show").text($row.find(".vehicle_no").html());
            $win.find("#driver_name_show").text($row.find(".driver_name").html());
            $win.find("#driver_phone_show").text($row.find(".driver_phone").html());


            $(["groups","vehicle_type","destination"]).each(function (index, name) {
                var val  = $row.find("." + name).val();
                $win.find("#" + name+"_show").text(val);
            });
            var address = $row.find(".address").attr("value");
            var speed =     $row.find(".speed").attr("value");
            //如果地址为空，则执行一次地址解析
            if(! address){
                var gps_str = $row.find(".vehicle_gps_*").attr("value");
                if(gps_str){
                    var gps_array = gps_str.split(',');
                    var longitude = gps_array[1];
                    var latitude = gps_array[2];
                    var speed =  gps_array[4];
                }
                //如果从mc中取得坐标无效，则使用数据库中备份的地址
                if( ! (longitude && latitude && $.trim(longitude) && $.trim(latitude)) ){
                    var longitude = $row.find(".longitude").attr('value');
                    var latitude = $row.find(".latitude").attr('value');
                }
                var gps = new Gps("", longitude, latitude, "", speed);
                gps.setLocation("address_show");
                $win.find(".speed_show").html(gps.speed);
                $row.find(".speed").val(gps.speed);
                $row.find(".address").val($win.find("#address_show").html());
            }else{
                $win.find(".speed_show").html(speed);
                $win.find(".address_show").html(address);
            }

        });

        $("[id^='line_']").live("mouseout", null, function () {
            $(".detailWin").hide();
            $(this).removeClass("ctn-h");
        });
        //鼠标移动到提示框上不消失
        var will_hide;
        $(".vehicle_row").live("mouseenter", null, function () {
            if(will_hide){
                clearTimeout(will_hide);
            }
        });
        $(".vehicle_row").live("mouseleave", null, function () {
            $(".detailWin").show();
            will_hide = setTimeout("$('.detailWin').hide()", 500);
        });
        $(".detailWin").hover(function () {
            if (will_hide) {
                clearTimeout(will_hide);
                $(".detailWin").show();
            }
        }, function () {
            $(".detailWin").hide();
        });

    }
);

// 为查询文本框添加回车事件
function SubmitKeyClick(obj, evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : "");
    var key_code = evt.keyCode ? evt.keyCode : (evt.which ? evt.which : evt.charCode);
    if (key_code == 13) {
        vehicle_search();
    }
}

function vehicle_search() {
    var vehicle_no = $("#vehicle_no").val();
    var driver_name = $("#driver_name").val();
    var group = $("#group_string").val();
    var destination = $("#destination_name").val();

    Ht.waitingBtn("query-vehicle", "查询中");

    show_shelter();

    $.get("/motorcade_maps/index",{vehicle_no: vehicle_no,driver_name: driver_name,group: group,destination: destination}, function(data){
        Ht.stopWaiting("query-vehicle");
        hide_shelter();

        $("#rst_info_view").html(data.html);
        show_notice(data.notice);
        resetMap();
        transGps2Location();
        markVehicle();
    }, "json");
}

//定义全局变量记录要查看的车号
var vehicleNo = "";

function motor_history(vehicleNo){
    if(!vehicleNo)
        return false;
     this.vehicleNo = vehicleNo;
    open_motor_his_win();
}

function   open_motor_his_win(){
    var height = $(window).height()/2 ;

    $(".time-range-win").css({"top": height, "left":330}).show();
}

function close_motor_his_win(){
    $(".time-range-win").hide();
}

function getPeriod(){
    var startTime = $("#start_time").val();
    var endTime = $("#end_time").val();

    if(startTime==""||startTime==null){
        alert("请选择开始时间！");
        return false;
    }
    if(endTime==""||endTime==null){
        alert("请选择结束时间！");
        return false;
    }
    var startNum = parseInt(startTime.replace(/-/g,''),10);
    var endNum = parseInt(endTime.replace(/-/g,''),10);
    if(startNum>endNum){
        alert("结束时间不能在开始时间之前！");
        return false;
    }
    return [startTime, endTime];
}
function sureToQueryHistory(){
    close_motor_his_win();
    var period = getPeriod();
    if(period)
        queryVehicleHistoryInPeriod(this.vehicleNo,period[0], period[1]);
}

function today_history(){
    close_motor_his_win();
    var endTime = new Date().format("yyyy-MM-dd hh:mm:ss");
    var oneDayBefore = new Date().valueOf() - 24 * 3600 * 1000;
    var startTime =  new Date(oneDayBefore).format("yyyy-MM-dd hh:mm:ss");
    console.log(startTime, endTime)
    queryVehicleHistoryInPeriod(this.vehicleNo,startTime, endTime);
}
function week_history(){
    close_motor_his_win();
    var endTime = new Date().format("yyyy-MM-dd hh:mm:ss");
    var oneWeekBefore = new Date().valueOf() - 7 * 24 * 3600 * 1000;
    var startTime =  new Date(oneWeekBefore).format("yyyy-MM-dd hh:mm:ss");
    queryVehicleHistoryInPeriod(this.vehicleNo,startTime, endTime);
}
function month_history(){
    close_motor_his_win();
    var endTime = new Date().format("yyyy-MM-dd hh:mm:ss");
    var time = new Date();
    var month = time.getMonth() - 1 ;
    time.setMonth(month);
    var startTime = time.format("yyyy-MM-dd hh:mm:ss");
    queryVehicleHistoryInPeriod(this.vehicleNo,startTime, endTime);

}
